package com.study.web.controller.app;

import java.util.List;
import java.util.Arrays;

import com.study.app.domain.AppFirmwareVersion;
import com.study.app.domain.bo.AppFirmwareVersionBo;
import com.study.app.domain.vo.AppFirmwareVersionVo;
import com.study.app.service.IAppFirmwareVersionService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.study.common.annotation.RepeatSubmit;
import com.study.common.annotation.Log;
import com.study.common.core.controller.BaseController;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.validate.AddGroup;
import com.study.common.core.validate.EditGroup;
import com.study.common.enums.BusinessType;
import com.study.common.utils.poi.ExcelUtil;
import com.study.common.core.page.TableDataInfo;

/**
 * app固件版本管理
 *
 * @author study
 * @date 2023-11-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/firmware/version")
public class AppFirmwareVersionController extends BaseController {

    private final IAppFirmwareVersionService iAppFirmwareVersionService;

    /**
     * 查询app固件版本管理列表
     */
    @SaCheckPermission("app:firmwareVersion:list")
    @GetMapping("/list")
    public TableDataInfo<AppFirmwareVersionVo> list(AppFirmwareVersionBo bo, PageQuery pageQuery) {
        return iAppFirmwareVersionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出app固件版本管理列表
     */
    @SaCheckPermission("app:firmwareVersion:export")
    @Log(title = "app固件版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppFirmwareVersionBo bo, HttpServletResponse response) {
        List<AppFirmwareVersionVo> list = iAppFirmwareVersionService.queryList(bo);
        ExcelUtil.exportExcel(list, "app固件版本管理", AppFirmwareVersionVo.class, response);
    }

    /**
     * 获取app固件版本管理详细信息
     *
     * @param versionId 主键
     */
    @SaCheckPermission("app:firmwareVersion:query")
    @GetMapping("/{versionId}")
    public R<AppFirmwareVersionVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long versionId) {
        return R.ok(iAppFirmwareVersionService.queryById(versionId));
    }

    /**
     * 新增app固件版本管理
     */
    @SaCheckPermission("app:firmwareVersion:add")
    @Log(title = "app固件版本管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppFirmwareVersionBo bo) {
        return toAjax(iAppFirmwareVersionService.insertByBo(bo));
    }

    /**
     * 修改app固件版本管理
     */
    @SaCheckPermission("app:firmwareVersion:edit")
    @Log(title = "app固件版本管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppFirmwareVersionBo bo) {
        return toAjax(iAppFirmwareVersionService.updateByBo(bo));
    }

    /**
     * 删除app固件版本管理
     *
     * @param versionIds 主键串
     */
    @SaCheckPermission("app:firmwareVersion:remove")
    @Log(title = "app固件版本管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{versionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] versionIds) {
        return toAjax(iAppFirmwareVersionService.deleteWithValidByIds(Arrays.asList(versionIds), true));
    }

    @SaCheckPermission("app:firmwareVersion:change")
    @Log(title = "app固件版本管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeForce")
    public R<Void> changeForce(@RequestBody AppFirmwareVersion firmwareVersion) {
        return toAjax(iAppFirmwareVersionService.changeForce(firmwareVersion));
    }

    @SaCheckPermission("app:firmwareVersion:change")
    @Log(title = "app固件版本管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeEnable")
    public R<Void> changeEnable(@RequestBody AppFirmwareVersion firmwareVersion) {
        return toAjax(iAppFirmwareVersionService.changeEnable(firmwareVersion));
    }

}
